#pragma once
#include <web/pch.h>
#include <web/TcpSocket.h>
#include <web/Socket.h>
#include <web/SockAddrIn.h>

namespace Web
{
    struct TcpServer : public Web::Socket
    {

        TcpServer()
        {
            InitTcp();
        }

        TcpServer(const unsigned int port)
        {
            InitTcp();
            Web::SockAddrIn sockAddrIn;
            sockAddrIn.SetFamily(AF_INET);
            sockAddrIn.SetAddr(INADDR_ANY);
            sockAddrIn.SetPort(port);

            Bind(sockAddrIn);
            Listen();
        }
        std::shared_ptr<Web::TcpSocket> Accept()
        {
            auto newSock=std::make_shared<Web::TcpSocket>();

            sockaddr_in &client_address = newSock->m_AddrIn;
            SOCKLEN client_address_size = sizeof(client_address);
            newSock->fd = accept(fd, reinterpret_cast<sockaddr *>(&client_address), &client_address_size);

            return newSock;
        }
    };
};
